import { groups, createGroup, follow, cancelFollow } from '../../../../api/user'
import Notify from '../../../../miniprogram_npm/@vant/weapp/notify/notify'

Component({
  properties: {
    detail: {
      type: Object,
      value: {}
    },
    isConsult: {
      type: Boolean,
      value: false
    },
    isAuth: {
      type: Boolean,
      value: true
    }
  },

  lifetimes: {
    attached() {
      this.getGroupList()
    }
  },

  attached() {
    this.getGroupList()
  },

  data: {
    groups: [],
    showGroups: false,
    showDialog: false,
    groupName: '',
    detail: {}
  },
  observers: {
    data: function(detail) {
      this.setData({
        detail: detail
      })
    }
  },
  methods: {
    getGroupList() {
      groups().then(res => {
        this.setData({
          groups: res
        })
      })
    },
    followTeacher() {
      this.setData({
        showGroups: true
      })
    },
    unFollowTeacher() {
      cancelFollow({
        followId: this.data.detail.followId
      }).then(res => {
        this.setData({
          'detail.isFollowed': false
        })
      })
    },
    unSelectGroup() {
      this.setData({
        showGroups: false
      })
    },
    selectGroup(e) {
      console.log(e.currentTarget.dataset)
      follow({
        followUserId: this.data.detail.userId,
        groupId: e.currentTarget.dataset.id
      }).then(res => {
        this.setData({
          showGroups: false,
          'detail.isFollowed': true,
          'detail.followId': res
        })
      })
    },
    addGroup(){
      this.setData({
        showDialog: true
      })
    },
    confirmCreate() {
      if (!this.data.groupName) {
        wx.showToast({
          title: '请输入分组名' ,
          icon: 'none'
        })
      } else {
        createGroup({ groupName: this.data.groupName }).then(res => {
          let groups = this.data.groups
          groups.push({
            id: res,
            name: this.data.groupName,
            special: false
          })
          this.setData({
            groups: groups,
            groupName: '',
            showDialog: false
          })
        })
      }
    },
    closeDialog() {
      this.setData({ showDialog: false });
    },
    setGroupName(e) {
      this.setData({
        groupName: e.detail.value
      })
    },
    consult() {
      wx.navigateTo({
        url: '/packageA/pages/msgRecords/msgRecords?userId=' + this.data.detail.userId
      })
    }
  }
})
